package com.hams.demo.system.entity.dto;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;

import com.hams.demo.system.entity.Department;

public class DepartmentQueryDTO {
	
	private String level;
	private String name;
	private String telephone;
	private String address;
	
	public String getLevel() {
		return level;
	}
	public void setLevel(String level) {
		this.level = level;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getTelephone() {
		return telephone;
	}
	public void setTelephone(String telephone) {
		this.telephone = telephone;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	
	public static Specification<Department> getSpecification(DepartmentQueryDTO departmentQueryDTO) {
		
		Specification<Department> spec = new Specification<Department>() {

			public Predicate toPredicate(Root<Department> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> list = new ArrayList<Predicate>();
				
				if(null != departmentQueryDTO && !StringUtils.isEmpty(departmentQueryDTO.getLevel())) {
					Predicate p1 = cb.like(root.get("level").as(String.class), "%"+departmentQueryDTO.getLevel()+"%");
					list.add(p1);
				}
				if(null != departmentQueryDTO && !StringUtils.isEmpty(departmentQueryDTO.getName())) {
					Predicate p2 = cb.like(root.get("name").as(String.class), "%"+departmentQueryDTO.getName()+"%");
					list.add(p2);
				}
				if(null != departmentQueryDTO && !StringUtils.isEmpty(departmentQueryDTO.getAddress())) {
					Predicate p3 = cb.like(root.get("address").as(String.class), "%"+departmentQueryDTO.getAddress()+"%");
					list.add(p3);
				}
				if(null != departmentQueryDTO && !StringUtils.isEmpty(departmentQueryDTO.getTelephone())) {
					Predicate p4 = cb.like(root.get("telephone").as(String.class), "%"+departmentQueryDTO.getTelephone()+"%");
					list.add(p4);
				}
				Predicate[] p = new Predicate[list.size()];
				return cb.and(list.toArray(p));
			}
			
		};
		return spec;
	}
	
	
}
